package com.hydra.base.redis;

import com.alibaba.fastjson2.JSONObject;
import com.hydra.base.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * redis操作类
 */
@Slf4j
@Component
public class RedisPlugin {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    public Object get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    public <T> T get(String key, Class<T> clazz) {
        Object value = get(key);
        if (value == null) {
            return null;
        } else if(value instanceof JSONObject) {
            return ((JSONObject)value).toJavaObject(clazz);
        } else if(value instanceof String) {
            return JsonUtil.parseObject((String)value, clazz);
        } else {
            throw new RuntimeException("处理redis返回json格式异常："+value);
        }
    }

    public Set<String> keys(String pattern) {
        if (pattern.startsWith("*")) {
            log.warn("禁止前匹配");
            return new HashSet<>();
        } else {
            return redisTemplate.keys(pattern);
        }
    }

    public void set(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public void set(String key, Object value, long seconds) {
        redisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
    }

    public void set(byte[] key, byte[] value) {
        redisTemplate.execute((RedisCallback<Object>) (con) -> {
            con.set(key, value);
            return null;
        });
    }

    public void setEx(String key, int seconds, Object value) {
        redisTemplate.opsForValue().set(key, value, seconds, TimeUnit.SECONDS);
    }

    public void setEx(byte[] key, int seconds, byte[] value) {
        redisTemplate.execute((RedisCallback<Object>) (con) -> {
            con.setEx(key, seconds, value);
            return null;
        });
    }

    public void setObject(String key, Object value) {
        setObject(key, value, -1);
    }

    public void setObject(String key, Object value, long seconds) {
        //转JSON字符串
        String jsonString = JsonUtil.toJsonString(value);
        redisTemplate.opsForValue().set(key, jsonString, seconds, TimeUnit.SECONDS);
    }

    // 删除key
    public void delete(String key){
        redisTemplate.delete(key);
    }

    // 删除多个key
    public void deleteKey (String ...keys){
        redisTemplate.delete(Arrays.asList(keys));
    }
    
    // 指定key的失效时间
    public void expire(String key,long time){
        redisTemplate.expire(key,time,TimeUnit.MINUTES);
    }

    // 根据key获取过期时间
    public long getExpire(String key){
        Long expire = redisTemplate.getExpire(key);
        return expire == null ?0 :expire;
    }

    // 判断key是否存在
    public boolean hasKey(String key){
        Boolean exist = redisTemplate.hasKey(key);
        return exist != null && exist;
    }

}
